package com.gcxy.shop.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gcxy.shop.common.CommonException;
import com.gcxy.shop.service.UserService;


@WebServlet(urlPatterns="/changepwd.do")
public class ChangepwdServlet extends HttpServlet{

	private static final long serialVersionUID = 1L;
	private Logger logger=LoggerFactory.getLogger(getClass());
	private UserService userService=new UserService();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug("开始执行dopost");
		try {
			//抽取参数,然后用string定义装起来
			String usercode=request.getParameter("usercode");
			String password=request.getParameter("password");
			String newpassword=request.getParameter("newpassword");
			String confirm=request.getParameter("confirm");
			logger.debug("抽取参数成功");
			//数据转换
			//数据校验
			if(password==null || newpassword==null || !newpassword.equals(confirm)) {
				throw new CommonException("密码长度不能为空，且两次输入密码必须相同");
			}
			//业务方法处理
			userService.changepwd(usercode,password,newpassword);
			//转发与重定向
			response.sendRedirect(request.getContextPath()+"/login.jsp");
			logger.debug("修改密码成功");
			
		} catch (Exception e) {
			request.setAttribute("error", e.getMessage());
			request.getRequestDispatcher("/changepwd.jsp").forward(request, response);
		}
	}
	

}
